package com.zcx.qshop.conn;

import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.http.AsyType;
import com.zcx.helper.http.note.HttpInlet;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/8/31 0031.
 */
@HttpInlet(Conn.INLET_JSON_GOODSATTRIBUTE)
public class JsonGoodsattributeAsyGet extends QSAsyGet {

    public String gid;

    public JsonGoodsattributeAsyGet(String gid, AsyCallBack<Info> asyCallBack) {

        super(asyCallBack);

        this.gid = gid;

        ASY_TYPE = AsyType.MAX_STALE;

    }

    @Override
    public Object parser(JSONObject object) {

        if(object.optString("message").equals("1")){

            Info info = new Info();

            info.title = object.optString("title");

            info.picurl = Conn.SERVICE + object.optString("picurl");

            info.price = object.optString("price");

            info.stock = object.optInt("stock");

            info.unit = object.optString("unit");

            info.attributeone = object.optString("attributeone");

            info.attributetwo = object.optString("attributetwo");

            JSONArray array = object.optJSONArray("attrionelist");

            for (int i = 0 ; i < array.length() ; i ++){

                Info.Attribute attribute = new Info.Attribute();

                JSONObject o = array.optJSONObject(i);

                attribute.id = o.optString("id");

                attribute.title = o.optString("title");

                info.attributeones.add(attribute);

            }

            array = object.optJSONArray("attritwolist");

            for (int i = 0 ; i < array.length() ; i ++){

                Info.Attribute attribute = new Info.Attribute();

                JSONObject o = array.optJSONObject(i);

                attribute.id = o.optString("id");

                attribute.title = o.optString("title");

                info.attributetwos.add(attribute);

            }

            return info;

        }

        return null;

    }

    public static class Info{

        public String title, picurl, price, unit, attributeone, attributetwo;

        public int stock;

        public List<Attribute> attributeones = new ArrayList<>();

        public List<Attribute> attributetwos = new ArrayList<>();

        public static class Attribute{

            public String id, title;

        }

    }

}
